package cn.qing.service.Impl;

import cn.qing.mapper.ProductMapper;
import cn.qing.pojo.Product;
import cn.qing.service.ProductService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;


    /**
     * 查找全部的并分页
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<Product> findAll(Integer page, Integer size) {
        //必须放一起，才能分页
        PageHelper.startPage(page,size);
        return productMapper.findAll();
    }

    /**
     * 添加一行
     * @param product
     */
    @Override
    public void save(Product product) {
        productMapper.save(product);
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        //要是关联的有表，就先把把关联表的外键变成null
        //例：productMapper.deleteByOrderId(id);
        productMapper.deleteById(id);
    }

    /**
     * 看详情
     * @param id
     * @return
     */
    @Override
    public Product findById(Integer id) {

        Product product = productMapper.findById(id);
        return product;
    }

    /**
     * 修改回显
     * @param id
     * @return
     */
    @Override
    public Product editById(Integer id) {
        Product product = productMapper.editById(id);

        return product;
    }

    @Override
    public void edit(Product product) {
        productMapper.edit(product);
    }
}
